package ManagersArea.ManagersAreaGUIS;

import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.event.*;
import javax.swing.border.TitledBorder;
import javax.swing.GroupLayout;
import java.util.EventObject;
import java.util.Date;
import com.toedter.calendar.JDateChooser;
import java.text.SimpleDateFormat;
import java.sql.*;
import java.text.*;
import javax.swing.JTable.*;
import java.awt.print.*;
import java.beans.*;
import ManagersArea.ManagersAreaDAOS.*;



public class PrintReportPanel extends JPanel implements ActionListener,PropertyChangeListener
{
	//attributes
	private JPanel basePanel = null,centerBasePanel = null,
	               reportChoosePanel = null,reportCenterPanel = null,reportBasePanel = null,reportNorthPanel = null,
	               generalPanel = null,DatePanel = null;
	private Box    southBasePanel = null;       
	               
	private JLabel dateToLabel = null,dateFromLabel = null,arrowLabel = null;
	private JDateChooser toDate = null,fromDate = null;
	private JLabel fromDateLabel = null,toDateLabel = null;
	private Date currentDate = null,dateTo = null,dateFrom = null;
	private SimpleDateFormat sdf = null;
	
	private JRadioButton summaryRB = null,currentStockOUT = null; 
	private JRadioButton topTenRentalsRB = null, topTenCustomerRB = null,oldStockRB = null,eventsRB = null;
	private ButtonGroup generalBG;
	
	private ButtonGroup specifiedBG = null;      
	               
	private JButton displayBut = null,printBut = null,clearBut = null;
              
    private JLabel reportPanelLabel = null;           
               
	private JLabel pageLabel = null,
	               dailySumLabel = null, totalRentsLabel = null,
	               topTenRLabel = null,topTenCustLabel = null,oldStockLabel = null,
	               jcbLabel = null;
	               
	private JComboBox eventsJCB = null;               
	               
	//***JTABLE FOR REPORTS***
	private JTable reportTable = null;
	private ReportsDAO repFunc = null;
	               
	private JTextArea reportJTA = null;
	private JScrollPane jsp = null;
	
	private JComboBox jcb1 = null,jcb2 = null,jcb3 = null;
	private JLabel jcbLabel1 = null,jcbLabel2 = null,jcbLabel3 = null;
	
	private JFrame testFrame;
	
	//colours & FOnts
	private Font butFont = null;
	private Font mainLabelFont = null;
	private Color customColour = null;
	private JLabel turtleLabel = null;
	
	
	//connection
	private Connection turtleConn = null;
	//constructor
	public PrintReportPanel(Connection inConn)
	{
		//connection
        turtleConn = inConn; 
       	repFunc = new ReportsDAO(turtleConn);
		
	    //colours & fonts
	    butFont = new Font("ButtonFont",Font.BOLD,15);
		mainLabelFont = new Font("MainFont",Font.BOLD,40);
	//	butFont = new Font("ButtonFont",Font.BOLD,20);
		customColour = Color.blue;
		
		//repFunc = new ReportsDAO();
		//center Base Panel*********************
		
		//+++general panel
		dailySumLabel = new JLabel("Total Sales");
		summaryRB = new JRadioButton();
		summaryRB.setSelected(true);
		totalRentsLabel = new JLabel("Current Stock OUT");
	    currentStockOUT = new JRadioButton();
	    topTenRLabel = new JLabel("Top 10 Rentals");
		topTenRentalsRB = new JRadioButton();
		topTenCustLabel = new JLabel("Top 10 Customers");
		topTenCustomerRB = new JRadioButton();
		oldStockLabel = new JLabel("Old Stock Check");
		oldStockRB = new JRadioButton();
		eventsJCB = new JComboBox();
		//eventsJCB.setMaximumSize(new Dimension(2,2));
		jcbLabel = new JLabel("-Choose Transaction-");
		jcbLabel.setForeground(Color.gray);
		jcbLabel.setFont(new Font("LabelFont",Font.ITALIC,12));
		eventsJCB.setPreferredSize(new Dimension(2,2));
		eventsJCB.addItem("Loan");
		eventsJCB.addItem("Return");
		eventsJCB.addItem("Credit");
		eventsJCB.addItem("Booking");
		eventsJCB.addItem("Fine");
		eventsJCB.addItem("Refund");
		eventsRB = new JRadioButton();
	    generalBG = new ButtonGroup();
	    generalBG.add(summaryRB);
	    generalBG.add(currentStockOUT);
	    generalBG.add(topTenRentalsRB);
		generalBG.add(topTenCustomerRB);
		generalBG.add(oldStockRB);
		generalBG.add(eventsRB);
		generalPanel = new JPanel(new GridLayout(0,2));
		generalPanel.add(dailySumLabel);
		generalPanel.add(summaryRB);
		generalPanel.add(totalRentsLabel);
		generalPanel.add(currentStockOUT);
		generalPanel.add(topTenRLabel);
		generalPanel.add(topTenRentalsRB);
		generalPanel.add(topTenCustLabel);
		generalPanel.add(topTenCustomerRB);
		generalPanel.add(oldStockLabel);
		generalPanel.add(oldStockRB);
		generalPanel.add(oldStockRB);
		generalPanel.add(jcbLabel);
		generalPanel.add(new JLabel(""));
		generalPanel.add(eventsJCB);
		generalPanel.add(eventsRB);
		generalPanel.setBorder(BorderFactory.createCompoundBorder(
			                   BorderFactory.createRaisedBevelBorder(),BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.white,Color.white),"Report Selection")));
		
		
		//+++DatePanel
		
		DatePanel = new JPanel();
	    toDateLabel = new JLabel("TO");
	    toDateLabel.setPreferredSize(new Dimension(20,60));
		fromDateLabel = new JLabel("FROM");
		fromDateLabel.setPreferredSize(new Dimension(20,60));
		currentDate = new Date();
		toDate = new JDateChooser(currentDate);
		toDate.addPropertyChangeListener(this);
		toDate.setMaximumSize(new Dimension(100,25));
		toDate.setPreferredSize(new Dimension(100,60));
		fromDate = new JDateChooser(currentDate);
		fromDate.addPropertyChangeListener(this);
		fromDate.setMaximumSize(new Dimension(100,25));
		fromDate.setPreferredSize(new Dimension(100,60));
		DatePanel.setBorder(BorderFactory.createCompoundBorder(
			                BorderFactory.createRaisedBevelBorder(),BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.white,Color.white),"Date Range")));
		
		GroupLayout layout  = new GroupLayout(DatePanel);
		layout.setAutoCreateContainerGaps(true);
		layout.setAutoCreateGaps(true);
		DatePanel.setLayout(layout);
		GroupLayout.SequentialGroup hGroup = layout.createSequentialGroup();
		
		hGroup.addGroup(layout.createParallelGroup().
        addComponent(fromDate).addComponent(toDate));
  	    hGroup.addGroup(layout.createParallelGroup().
        addComponent(fromDateLabel).addComponent(toDateLabel));
   		layout.setHorizontalGroup(hGroup);
		
		GroupLayout.SequentialGroup vGroup = layout.createSequentialGroup();
		vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(fromDate).addComponent(fromDateLabel));
   		vGroup.addGroup(layout.createParallelGroup(GroupLayout.Alignment.BASELINE,true).
        addComponent(toDate).addComponent(toDateLabel));
        layout.setVerticalGroup(vGroup);
        
		reportChoosePanel = new JPanel(new GridLayout(0,1));
		reportChoosePanel.add(generalPanel);
		reportChoosePanel.add(DatePanel);
		
		
		jsp = new JScrollPane(ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_ALWAYS);
		reportBasePanel = new JPanel(new BorderLayout());
		reportPanelLabel = new JLabel("Report :");
		reportNorthPanel = new JPanel();
		BoxLayout b1 = new BoxLayout(reportNorthPanel,BoxLayout.X_AXIS);
		reportNorthPanel.setLayout(b1);
		dateToLabel = new JLabel(" Date ");
		arrowLabel = new JLabel(new ImageIcon("C:/javausers/IconImages/RightSmallArrow.png"));
		dateFromLabel = new JLabel(" Date ");
		dateToLabel.setFont(butFont);
		dateFromLabel.setFont(butFont);
		reportNorthPanel.add(dateFromLabel);
		reportNorthPanel.add(arrowLabel);
		reportNorthPanel.add(dateToLabel);
		reportNorthPanel.setBorder(BorderFactory.createEmptyBorder(20,110,10,0));
		reportCenterPanel = new JPanel(new GridLayout(1,1));
	    jsp.setBackground(Color.white);
		reportCenterPanel.setBorder(BorderFactory.createEmptyBorder(30,30,20,0));
		reportCenterPanel.add(jsp);
		
		reportBasePanel.setBorder(BorderFactory.createCompoundBorder(
			                      BorderFactory.createRaisedBevelBorder(),BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.white,Color.black),"REPORTS")));
		reportBasePanel.add(reportCenterPanel,BorderLayout.CENTER);
		reportBasePanel.add(reportNorthPanel,BorderLayout.NORTH);
		centerBasePanel = new JPanel(new BorderLayout());
		centerBasePanel.add(reportBasePanel,BorderLayout.CENTER);
		centerBasePanel.add(reportChoosePanel,BorderLayout.WEST);
		
		//***************************************************************
		
		//southBasePanel
		printBut = new JButton("Print");
		printBut.setEnabled(false);
		printBut.setBorder(BorderFactory.createRaisedBevelBorder());
		printBut.setFont(butFont);
		printBut.setForeground(Color.black);
		displayBut = new JButton("Display");
		displayBut.setBorder(BorderFactory.createRaisedBevelBorder());
		displayBut.setFont(butFont);
		displayBut.setForeground(Color.black);
		displayBut.addActionListener(this);
		printBut.addActionListener(this);
		clearBut = new JButton("Clear");
		clearBut.setBorder(BorderFactory.createRaisedBevelBorder());
		clearBut.setFont(butFont);
		clearBut.setForeground(Color.black);
		clearBut.addActionListener(this);
		
		southBasePanel = new Box(BoxLayout.X_AXIS);
		southBasePanel.add(Box.createHorizontalStrut(60));
		southBasePanel.add(displayBut);
		southBasePanel.add(Box.createHorizontalStrut(5));
		southBasePanel.add(clearBut);
		southBasePanel.add(Box.createHorizontalStrut(400));
		southBasePanel.add(printBut);
		
	    //main Base Panel
		basePanel = new JPanel(new BorderLayout());
		//basePanel.add(pageLabel,BorderLayout.NORTH);
		basePanel.add(centerBasePanel);
		basePanel.add(southBasePanel,BorderLayout.SOUTH);
		basePanel.setBorder(BorderFactory.createTitledBorder(BorderFactory.createEtchedBorder(Color.white,Color.black),""));
		                    
		
		//setup This panel
		this.setLayout(new GridLayout(1,1));
		this.add(basePanel);
		
	}//end constructor
	
	public void actionPerformed(ActionEvent e)
	{
		Object source = e.getSource();
	
	    if(source == printBut)
		{
			System.out.println("in print");
			try
			{
				reportTable.print(PrintMode.FIT_WIDTH,new MessageFormat(getDateLabel()),
				                  new MessageFormat("JAMES SMELLS...CAN SOMEONE PLEASE SWOP SEATS WITHME?? I NEED TO GET AWAY"));
			}
			catch(PrinterException pe)
			{
				pe.printStackTrace();
			}
		}
		if(source == clearBut)
		{
			clearReports();
			printBut.setEnabled(false);
		}
			
	
		if(source == displayBut)
		{
			sdf = new SimpleDateFormat("yyyy.MM.dd");
			dateFrom = fromDate.getDate();
			dateTo = toDate.getDate();
			
			System.out.println("to date: " + dateTo + " From date: " + dateFrom);
			
			if(topTenCustomerRB.isSelected() == true)
			{
				setDateLabel("-Top Ten Customers-  ");
				reportTable = repFunc.topTenCust(dateFrom,dateTo);
				System.out.println(reportTable.getColumnName(2));
                jsp = null;
                jsp = new JScrollPane(reportTable,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                reportCenterPanel.removeAll();
                reportCenterPanel.add(jsp);
				this.validate();
				printBut.setEnabled(true);
				//repFunc = null;
			}//check for checkBoxes	
			
			if(topTenRentalsRB.isSelected() == true)
			{
				setDateLabel("-Top Ten Rentals-  ");
				reportTable = repFunc.topTenRentals(dateFrom,dateTo);
                jsp = null;
                jsp = new JScrollPane(reportTable,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                reportCenterPanel.removeAll();
                reportCenterPanel.add(jsp);
				this.validate();
				printBut.setEnabled(true);
				//repFunc = null;
			}//check for checkBoxes	
			if(currentStockOUT.isSelected() == true)
			{
				dateToLabel.setText("Current Stock Unavailable");
		    	dateFromLabel.setText("");
				reportTable = repFunc.getStockOut();
                jsp = null;
                jsp = new JScrollPane(reportTable,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                reportCenterPanel.removeAll();
                reportCenterPanel.add(jsp);
				this.validate();
				printBut.setEnabled(true);
				//repFunc = null;
			}
			if(summaryRB.isSelected() == true)
			{
				setDateLabel("-Financial Summary-  ");
				reportTable = repFunc.doSummary(dateFrom,dateTo);
                jsp = null;
                jsp = new JScrollPane(reportTable,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                reportCenterPanel.removeAll();
                reportCenterPanel.add(jsp);
				this.validate();
				printBut.setEnabled(true);
				//repFunc = null;
			}
			if(oldStockRB.isSelected() == true)
			{
				dateToLabel.setText("Stock Not Rented in 6Months or more");
		    	dateFromLabel.setText("");
				reportTable = repFunc.getOldStock();
                jsp = null;
                jsp = new JScrollPane(reportTable,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                reportCenterPanel.removeAll();
                reportCenterPanel.add(jsp);
				this.validate();
				printBut.setEnabled(true);
				//repFunc = null;
			}
			if(eventsRB.isSelected() == true)
			{
				
		    	String selItem = (String)eventsJCB.getSelectedItem();
		    	setDateLabel("Transactions(" +selItem  +")  ");
				reportTable = repFunc.getEvents(selItem,dateFrom,dateTo);
                jsp = null;
                jsp = new JScrollPane(reportTable,ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS,ScrollPaneConstants.HORIZONTAL_SCROLLBAR_AS_NEEDED);
                reportCenterPanel.removeAll();
                reportCenterPanel.add(jsp);
				this.validate();
				printBut.setEnabled(true);
				//repFunc = null;
			}
			
		}//end if displayBut
		
	}//end action Performed
	
	public void propertyChange(PropertyChangeEvent pe)
	{
		Object source = pe.getSource();
		if(source == fromDate)
		{
			if(toDate.getDate().compareTo(fromDate.getDate()) < 1)
			{ 
				toDate.setDate(fromDate.getDate());
				toDate.setMinSelectableDate(fromDate.getDate());
			}
			else
			{
				toDate.setMinSelectableDate(fromDate.getDate());
			}
				
		}
		if(source == toDate)
		{
			
		}
		

	}
	
	//general methods
	public void clearReports()
	{
		setDateLabel("-Date-  ");
        jsp = null;
        reportCenterPanel.removeAll();
		this.validate();
		this.repaint();
	}
	public void setDateLabel(String text1)
	{
		//printFooterText  = text1;
		dateToLabel.setText(" "+sdf.format(dateTo)+" ");
	    dateFromLabel.setText(text1 +"\n "+sdf.format(dateFrom) +" ");
	}
	public String getDateLabel()
	{
		String printHeader = ""+dateFromLabel.getText()+ " to " +dateToLabel.getText();
		return printHeader;
	}
}//end panel